namespace EyeOpen.ModularDemo.Mvc
{
	using System;
	using System.IO;
	using System.Web;

	using EyeOpen.Security.Data;
	using EyeOpen.Security.Domain;

	public class DatabaseCreateAndPopulate
	{
		public void Create(ISecurityUnitOfWork securityUnitOfWork)
		{
			using (securityUnitOfWork)
			{
				securityUnitOfWork
					.Database
					.CreateIfNotExists(
						() =>
							{
								var dataDirectory = HttpContext.Current.Server.MapPath("~/App_Data");
								Directory.CreateDirectory(dataDirectory);
							},
							() => Populate(securityUnitOfWork));
			}
		}

		private void Populate(ISecurityUnitOfWork securityUnitOfWork)
		{
			securityUnitOfWork
				.UserRepository
				.Add(
					new User
						{
							Id = new Guid("494230dd-9f1c-4623-9563-308a0bdac050"),
							Email = "admin@domain.com",
							Password = User.CreatePassword("password")
						});

			securityUnitOfWork.Commit();
		}
	}
}